﻿using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ThridGroup.ERP.DTO;
using ThridGroup.ERP.WarehouseManagement;

namespace ThridGroup.ERP.Web.Controllers.WarehouseManagement
{
    public class SupplierReturnsController : Controller
    {
        public IActionResult SupplierReturnsView()
        {
            return View();
        }

        public IActionResult SupplierReturnsEditView(int? id)
        {
            ViewBag.Id = id;
            return View();
        }

        private readonly ISupplierReturnsServices supplierReturnsServices;

        public SupplierReturnsController(ISupplierReturnsServices supplierReturnsServices)
        {
            this.supplierReturnsServices = supplierReturnsServices;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="supplierReturns"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddSupplierReturns([FromBody]SupplierReturnsDTO supplierReturns)
        {
            return await supplierReturnsServices.AddSupplierReturns(supplierReturns);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task DelSupplierReturns([FromBody]List<int> ids)
        {
            await supplierReturnsServices.DelSupplierReturns(ids);
        }
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<SupplierReturnsDTO> GetSupplierReturns(int id)
        {
            return await supplierReturnsServices.GetSupplierReturns(id);
        }
        /// <summary>
        /// 列表显示
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="returnName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> ShowSupplierReturns(int pageIndex = 1, int pageSize = 10, string? returnName = "")
        {
            var list=await supplierReturnsServices.ShowSupplierReturns(pageIndex, pageSize, returnName);
            var result=list.OrderByDescending(m=>m.Id).Skip((pageIndex-1)*pageSize).Take(pageSize).ToList();
            return Json(new
            {
                code=0,
                msg="",
                count=list.Count,
                data=result
            });
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="supplierReturns"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> UpdateSupplierReturns([FromBody]SupplierReturnsDTO supplierReturns)
        {
            return await supplierReturnsServices.UpdateSupplierReturns(supplierReturns);
        }

       
    }
}
